import 'package:flutter/material.dart';
import 'package:flutter_jscore/flutter_jscore.dart';
import 'dart:convert';
import 'dart:io';

import './view.dart';
void main() => runApp(MyApp());
var httpClient = new HttpClient();
  // 网络请求
  getIPAddress() async {
    var url = 'https://httpbin.org/ip';
    var httpClient = new HttpClient();

    String result;
    try {
      var request = await httpClient.getUrl(Uri.parse(url));
      var response = await request.close();
      if (response.statusCode == HttpStatus.OK) {
        var json = await response.transform(utf8.decoder).join();
        var data = jsonDecode(json);
        result = data['origin'];
      } else {
        result =
            'Error getting IP address:\nHttp status ${response.statusCode}';
      }
    } catch (exception) {
      result = 'Failed getting IP address';
    }
    print("请求成功" + result);
  }

void runJS() {
    JSContext jsContext = JSContext.createInGroup();
    // Replace '1 + 1' with your JavaScript code
    JSValue jsValue = jsContext.evaluate('1 + 1');
    print(jsValue.string);

    JSValue value1 = jsContext.evaluate("234 + 89");
    print(value1.string);
    jsContext.release();
}
// 封装首页内容
class MyHomeView extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('Welcome to Flutter1'),
        ),
        body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
              Text('Welcome to Flutter1'),
              RaisedButton(
                child: Text("点击发送请3求"),
                onPressed: getIPAddress,
              ),
              RaisedButton(
                onPressed: runJS,
                child: Text('运行2'),
              ),
              RaisedButton(
                onPressed: () {
                  Navigator.push(context, MaterialPageRoute(builder: (context) {
                    return SecondScreen();
                  }));
                },
                child: Text("跳转到webview页面"),
              ),

        ])),
      );
  }

}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Welcome to Flutter',
      home: new MyHomeView(),
    );
  }
}
